package cn.ekuma.epos.crm.service;

import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.ofsm.IStateChangeListener;
import org.ofsm.IStateTransition;
import com.openbravo.bean.AppUser;
import com.openbravo.bean.crm.service.Telephone;
import com.openbravo.bean.crm.service.TelephoneFollow;
import com.openbravo.bean.erp.OrderState;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JObjectListFinder;
import cn.ekuma.data.ui.swing.AbstractChildParentDAOJEditor;
import cn.ekuma.epos.crm.service.model.TelephoneFollowTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.panel.JStateObjectChildDAOEditorPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelephoneEditor extends AbstractChildParentDAOJEditor<Telephone,String> implements IStateChangeListener{

	AppUser user;
	I_DataLogicSystem dlSystem;
	JStateObjectChildDAOEditorPanel followPanel;
	private JTextField txtCreateTime;
	private JTextField txtPriority;
	private JTextField txtRelationMan;
	private JTextField txtUser;
	private JTextField txtMode;
	private JTextField txtNextTime;
	private JTextField txtState;
	private JTextField txtEndTime;
	private JTextField txtSubject;
	private JTextArea txtSubjectDesc;
	private List<Object> allUser;
	private I_DataLogicERP dlSales;
	private JButton btnP;
	private JButton btnT;
	
	public TelephoneEditor(DirtyManager dirty,AppUser user,I_DataLogicSystem dlSystem,I_DataLogicERP dlSales) {
		super(dirty);
		this.user=user;
		this.dlSystem=dlSystem;
		this.dlSales=dlSales;
		followPanel=new JStateObjectChildDAOEditorPanel<String>(new TelephoneFollowTableModel(),user,dlSystem,"TelephoneFollowStateProcess");
		followPanel.addStateChangeListener(this);
		initComponents();
		add(followPanel,BorderLayout.CENTER);
		addObjectChangeListener(followPanel);
		
		
		txtSubject.getDocument().addDocumentListener(dirty);
		txtSubjectDesc.getDocument().addDocumentListener(dirty);
		txtMode.getDocument().addDocumentListener(dirty);
		txtNextTime.getDocument().addDocumentListener(dirty);
		txtPriority.getDocument().addDocumentListener(dirty);
		txtEndTime.getDocument().addDocumentListener(dirty);
	}
	
	@Override
	public void activate() throws BasicException {
		allUser = dlSales.listPeopleVisible();
	}

	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 200));
		panel_3.add(panel, BorderLayout.NORTH);
		panel.setLayout(null);
		
		JLabel label = new JLabel(AppLocal.getIntString("label.StateObject.subject"));
		label.setBounds(10, 10, 87, 15);
		panel.add(label);
		
		txtSubject = new JTextField();
		txtSubject.setBounds(107, 7, 507, 21);
		panel.add(txtSubject);
		txtSubject.setColumns(10);
		
		JLabel label_4 = new JLabel(AppLocal.getIntString("label.StateObject.userMan"));
		label_4.setBounds(369, 41, 99, 15);
		panel.add(label_4);
		
		txtUser = new JTextField();
		txtUser.setEditable(false);
		txtUser.setBounds(478, 38, 136, 21);
		panel.add(txtUser);
		txtUser.setColumns(10);
		
		JLabel label_1 = new JLabel(AppLocal.getIntString("label.StateObject.callTime"));
		label_1.setBounds(10, 41, 87, 15);
		panel.add(label_1);
		
		txtCreateTime = new JTextField();
		txtCreateTime.setEditable(false);
		txtCreateTime.setBounds(107, 38, 205, 21);
		panel.add(txtCreateTime);
		txtCreateTime.setColumns(10);
		
		txtSubjectDesc = new JTextArea();
		txtSubjectDesc.setBorder(new TitledBorder(null, AppLocal.getIntString("label.StateObject.subjectDesc"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		txtSubjectDesc.setBounds(10, 66, 604, 112);
		panel.add(txtSubjectDesc);
		
		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(10, 80));
		panel_3.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel label_3 = new JLabel(AppLocal.getIntString("label.StateObject.relationMan"));
		label_3.setBounds(10, 10, 85, 15);
		panel_1.add(label_3);
		
		txtRelationMan = new JTextField();
		txtRelationMan.setEditable(false);
		txtRelationMan.setBounds(105, 10, 213, 21);
		panel_1.add(txtRelationMan);
		txtRelationMan.setColumns(10);
		
		JLabel label_5 = new JLabel(AppLocal.getIntString("label.Telephone.mode"));
		label_5.setBounds(377, 10, 93, 15);
		panel_1.add(label_5);
		
		txtMode = new JTextField();
		txtMode.setBounds(480, 10, 136, 21);
		panel_1.add(txtMode);
		txtMode.setColumns(10);
		
		JLabel label_6 = new JLabel(AppLocal.getIntString("label.StateObject.nextTime"));
		label_6.setBounds(10, 40, 85, 15);
		panel_1.add(label_6);
		
		txtNextTime = new JTextField();
		txtNextTime.setBounds(105, 41, 213, 21);
		panel_1.add(txtNextTime);
		txtNextTime.setColumns(10);
		
		 btnP = new JButton("");
		btnP.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 JObjectListFinder userList = JObjectListFinder.newJDialog(TelephoneEditor.this);
				 UserInfo tempUser = (UserInfo) userList.showUserList(allUser);
                 if(tempUser!=null){
                	 obj.setRelationID(tempUser.getId());
             		 obj.setRelationMan(tempUser.getName());
             		 txtRelationMan.setText(obj.getRelationMan());
                	 dirty.setDirty(true);
                 }
			}
		});
		btnP.setIcon(new ImageIcon(TelephoneEditor.class.getResource("/com/openbravo/images/kdmconfig.png")));
		btnP.setBounds(328, 9, 39, 23);
		panel_1.add(btnP);
		
		 btnT = new JButton("");
		btnT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateStartActionPerformed(e);
			}
		});
		btnT.setIcon(new ImageIcon(TelephoneEditor.class.getResource("/com/openbravo/images/1day.png")));
		btnT.setBounds(328, 36, 39, 23);
		panel_1.add(btnT);
		
		JLabel label_2 = new JLabel(AppLocal.getIntString("label.StateObject.priority"));
		label_2.setBounds(377, 41, 54, 15);
		panel_1.add(label_2);
		
		txtPriority = new JTextField();
		txtPriority.setBounds(480, 35, 136, 21);
		panel_1.add(txtPriority);
		txtPriority.setColumns(10);
		
		JPanel panel_2 = new JPanel();
		panel_2.setPreferredSize(new Dimension(10, 60));
		panel_3.add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(null);
		
		JLabel label_8 = new JLabel(AppLocal.getIntString("label.StateObject.endTime"));
		label_8.setBounds(10, 13, 99, 15);
		panel_2.add(label_8);
		
		txtEndTime = new JTextField();
		txtEndTime.setBounds(109, 10, 210, 21);
		panel_2.add(txtEndTime);
		txtEndTime.setColumns(10);
		
		JLabel label_7 = new JLabel(AppLocal.getIntString("label.StateObject.state"));
		label_7.setBounds(379, 13, 106, 15);
		panel_2.add(label_7);
		
		txtState = new JTextField();
		txtState.setEditable(false);
		txtState.setBounds(483, 10, 115, 21);
		panel_2.add(txtState);
		txtState.setColumns(10);
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

	private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(txtNextTime.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			txtNextTime.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}
	
	@Override
	public Telephone createNew(Properties prop) {
		Telephone newObj=new Telephone();
		if(parentNode!=null)
			newObj.setCustomerId(parentNode.getParentKey().getKey());
		newObj.setCallTime(new Date());
		newObj.setPriority(Telephone.PRIORITY_A);
		newObj.setUserId(user.getId());
		newObj.setUserMan(user.getName());
		newObj.setRelationID(user.getId());
		newObj.setRelationMan(user.getName());
		newObj.setState(AppLocal.getIntString(OrderState.ORDERSTATE_Start));
		return newObj;
	}

	@Override
	public void rebuild(Telephone obj) throws BasicException {
		obj.setSubject(txtSubject.getText());
		obj.setSubjectDesc(txtSubjectDesc.getText());
	    obj.setMode(txtMode.getText());
	    obj.setNextTime((Date) Formats.TIMESTAMP.parseValue(txtNextTime.getText()));
	    obj.setPriority((Integer) Formats.INT.parseValue(txtPriority.getText()));  	
	    obj.setEndTime((Date) Formats.TIMESTAMP.parseValue(txtEndTime.getText()));
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtSubject.setText(obj.getSubject());
		txtSubjectDesc.setText(obj.getSubjectDesc());
		txtMode.setText(obj.getMode());
		txtNextTime.setText(Formats.TIMESTAMP.formatValue(obj.getNextTime()));
		txtPriority.setText(Formats.INT.formatValue(obj.getPriority()));
		
		txtUser.setText(obj.getUserMan());
		txtCreateTime.setText(Formats.TIMESTAMP.formatValue(obj.getCallTime()));
		txtEndTime.setText(Formats.TIMESTAMP.formatValue(obj.getEndTime()));
		txtState.setText(obj.getState());
		
		txtRelationMan.setText(obj.getRelationMan());
		
		txtSubject.setEditable(state==this.STATE_Insert);
		txtSubjectDesc.setEditable(editAble);
		txtMode.setEditable(editAble);
		txtNextTime.setEditable(state==this.STATE_Insert);
		txtPriority.setEditable(state==this.STATE_Insert);
		btnP.setEnabled(editAble);
		btnT.setEnabled(editAble);
		txtEndTime.setEditable(editAble);
	}

	@Override
	public Class getEditClass() {
		return Telephone.class;
	}

	@Override
	public String getTabName() {
		return "label.Telephone";
	}


	@Override
	public boolean onStateChanging(IStateTransition transition) {
		return true;
	}


	@Override
	public void onStateChanged(IStateTransition transition, String memo) {
		TelephoneFollow telephoneFollow=new TelephoneFollow();
		try {
			followPanel.updateObj(telephoneFollow);
			dlSales.updateTelephone(obj,telephoneFollow);
			updateUI(state==this.STATE_Edit, state);
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
